<?php
class UsersForm extends BaseForm {
	
	private $users; 
	private $selected;
	private $id;  
	
	public function UsersForm(){
		parent::BaseForm();
		
		$this->users = array();
		$this->selected = array();
	}
	
	public function clear(){
		$this->users = array();
		$this->selected = array();
		
		$this->setId(null);
	}
	
	public function getUsers(){ return $this->users; }
	public function getSelected(){ return $this->selected; }
	
	public function setUsers($val){ $this->users = $val; }
	public function setSelected($val){ $this->selected = $val; }
	
	public function setId($val){ $this->id = $val; }
	public function getId(){ return $this->id; }
	
	public function getUserById($id){
		$res = null;
		
		foreach($this->getUsers() as $current){
			if($current->getId() == $id){
				$res = $current; 
				
				break;
			}
		}
		
		return $res;
	}
	
	public function deleteUsers($selected){
		$sel = array_flip($selected);
		$arr = array();
		
		foreach($this->getUsers() as $current){
			$id = $current->getId();
			
			if(! array_key_exists($id, $sel)){
				$arr[] = $current;
			}
		}
		
		// set the users as the remaining ones
		$this->setUsers($arr);
	}
	
	public function addUser($user){
		$this->users[] = $user;
	}
	
	public function updateUser($user){
		$existent = $this->getUserById($user->getId());
		
		if($existent != null){
			$existent->copy($user);
		}
	}
		
} 
?>